package hw;

import java.io.*;

public class Test1 {

    public void copy() {
        FileReader fr = null;
        BufferedReader br = null;
        String str1 = null;
        String str2 = null;
        try {
            fr = new FileReader("D:\\1.txt");
            br = new BufferedReader(fr);
            while ((str1 = br.readLine()) != null) {
                str2 = str1;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //复制
        FileWriter fw = null;
        try {
            fw = new FileWriter("d:/2.txt");
            fw.write(str2);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //在文件最后追加一段字符串
    public void append() {
        FileWriter fw = null;
        try {
            fw = new FileWriter("d:\\1.txt", true);  //两个参数，一个文件名，一个指示是否附加写入的数据
            fw.append("————追加的内容");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Test1 test1 = new Test1();
        test1.copy();
        test1.append();
    }
}
